<?php

if(!defined('IN_SYS')) exit('Access Denied');

class system_controller extends admincp{

	public function config(){
		if(IS_POST){
			$data = gp("data", false);
			if(isset($data['webHost']) && empty($data['webHost'])){
				$data['webHost'] = $_SERVER['HTTP_HOST'];
			}
			foreach ($data as $key => $value) {
				if (isset($GLOBALS['G'][$key])) {
					db::query("update config set varValue='$value' where varName='$key' ");
				} else {
					db::query("delete from config where varName='$key'");
					db::query("insert config set varValue='$value',varName='$key'");
				}	
			}
			self::json( config::update_config() );
		}else{
			$r = db::select("select * from config");
			$var = array();
			foreach ($r as $key => $value) {
				$var[$value['varName']] = $value['varValue'];
			}
			require self::tpl("system_config");
		}
	}

	public function index(){
		$this->config();
	}

	public function configStep2(){
		$ref = gp('ref', false);
		if(IS_POST){
			$r = gp("username,password");
			$data = array();
			$data['username'] = $r['username'];
			$data['password'] = $r['password'];
			$data['appid'] = db::getfield("select varValue from config where varName='appid'");
			$toUrl = REMOTEAPI."/sync/updateConfigDate";
            $res = config::update_config();
            self::json( array('status' => 1, 'msg' => '配置修改成功', 'redirect' => $ref) );
		}else{
			$username = gp('u');
			require self::tpl("system_configStep2");
		}
	}

	public static function delcache($dir) {
		if(!is_dir($dir)) return;
		$dh=opendir($dir);
		while ($file = readdir($dh)) {
			if($file != "." && $file != "..") {
				$fullpath = $dir."/".$file;
				if(!is_dir($fullpath)) {
					unlink($fullpath);
				} else {
					self::delcache($fullpath);
				}
			}
		}
		closedir($dh);
		// rmdir($dir);
	}

}

